

import '../../../utils/Settings.dart';
import '../../../utils/Utils.dart';
import '../../form/LFormLogic.dart';
import '../../get/MyController.dart';
import 'package:get/get.dart';

import '../../queryform/LQueryFormLogic.dart';
import 'LFormInputState.dart';
class LFormInputLogic extends MyController{

  final state = LFormInputState();

  void init({required Map formItem,required Map formData,String? betweenSign,required String formTag,required String formType}){
    state.formItem = formItem;
    state.formData = formData;
    state.betweenSign = betweenSign??"";
    state.formTag = formTag;
    state.formType = formType;
  }

  void onChanged(v){
    state.formData['${state.formItem["field"]}${state.betweenSign}']=v;
    if(state.formType==Settings.formType["表单"]){
      Get.find<LFormLogic>(tag: state.formTag).onChange({'field':'${state.formItem["field"]}${state.betweenSign}','val':v});
    }else if(state.formType==Settings.formType["查询表单"]){
      Get.put(LQueryFormLogic(),tag:state.formTag).onChange({'field':'${state.formItem["field"]}${state.betweenSign}','val':v});
    }
  }

  void onSaved(v){
    state.formData['${state.formItem["field"]}${state.betweenSign}']=v?.trim();
  }

  dynamic onValidator(v){
    //非空验证
    if(state.formItem["required"]==true && (v==null || v.isEmpty || v.trim().isEmpty)){
      return "${state.formItem["label"]}不能为空";
    }
    //正则验证
    if(!Utils.isEmpty(state.formItem["rules"]) && !Utils.isEmpty(state.formItem["rules"]["regExp"])){
      RegExp regExp = RegExp(state.formItem["rules"]["regExp"]);
      if (!regExp.hasMatch(v.toString())){
        return state.formItem["rules"]["message"]??"${state.formItem["label"]}输入不符合要求";
      }
    }
    return null;
  }


  ///如果是QUERYFORM 重置，需要将显示值清空
  @override
  void reset(){}

}